//
//  PVDEditChecklistTextView.swift
//  GreenDeal
//
//  Created by Moennig Q on 13/2/2023.
//

import UIKit
import RxCocoa
import RxSwift
class PVDEditChecklistTextView: GreenNaviBarView {
    lazy var disposeBag = DisposeBag()
    var saveBtn = OperationButton(R.string.localizable.save())
    let msgTextView = TextViewWithRequiredView()
    
    override func setUpView() {
        //contentBackView
        let contentBackView = View(hexStr: "#FFFFFFFF")
        contentBackView.drawCorner(byRoundingCorners: [.topLeft,.topRight],radii: 16)
        contentBackView.backgroundColor = .white
        self.addSubview(contentBackView)
        contentBackView.snp.makeConstraints { make in
            make.top.equalTo(topBackView.snp.bottom).offset(-20)
            make.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        //msgTextView
        contentBackView.addSubview(msgTextView)
        msgTextView.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.greaterThanOrEqualTo(0)
        }
        //save btn
        contentBackView.addSubview(saveBtn)
        saveBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(OperationButton.bottomMargin)
            make.left.right.equalTo(msgTextView)
            make.height.equalTo(OperationButton.height)
        }
        self.msgTextView.textView.placeholder = "Please input message, this cannot be empty"
        self.msgTextView.textView.placeholdColor = UIColor(hexStr: "#292D3266")
        self.msgTextView.textView.placeholdFont = R.font.interRegular(size: 14)
        self.msgTextView.textView.textColor = .black
        self.msgTextView.textView.font = R.font.interRegular(size: 15)
        //
        self.msgTextView.endEditingClosure = {[weak self] (content) in
            guard let strongSelf = self else {return}
            if content.count > 0 {
                strongSelf.saveBtn.enableBtn()
            }else{
                strongSelf.saveBtn.disableBtn()
            }
            
        }
    }
    func setData(_ checkModel: PVDChecklistModel){
        self.msgTextView.needCheckFlag = (checkModel.is_required == "true" ? true : false)//必填项才校验，非必填项不校验
        self.msgTextView.textView.text = checkModel.answer
        var editFlag: Bool = false
        if checkModel.jobType == .pvdJob {//pvd Job
            editFlag = JobDetailModelHelper.shared.jobDetailModel.canEditJobDetailFlag
        }else{//service job
            editFlag = ServiceDetailModelHelper.shared.serviceDetailModel.canEditJobDetailFlag
        }
        
        self.saveBtn.isHidden = !editFlag
        self.msgTextView.textView.isEditable = editFlag
        self.msgTextView.textView.textChange(Notification(name: UITextView.textDidChangeNotification))
        //
        self.msgTextView.checkTextViewIfValid()
        if self.msgTextView.isValidFlag == false{
            self.saveBtn.disableBtn()
        }
    }
}
